//
//  SJDataManageTool.m
//  Test
//
//  Created by zhaoshijie on 24/12/2017.
//  Copyright © 2017 zhaoshijie. All rights reserved.
//

#import "SJDataManageTool.h"

@implementation SJDataManageTool


/**
 Json字符串转换成Json对象

 @param jsonString Json字符串
 @return Json对象
 */
+ (id)jsonStringConvertToJosnClassWithJsonString:(NSString *)jsonString
{
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id jsonClass = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    
    return jsonClass;
}

/**
 Json对象转换成Json字符串
 
 @param jsonClass Json对象
 @return Json字符串
 */
+ (NSString *)jsonClassConvertToJosnStringWithJsonClass:(id)jsonClass
{
    NSString *resultString;
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonClass options:NSJSONWritingPrettyPrinted error:&error];
    if (error == nil)
    {
        resultString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    return resultString;
}


/**
 Json对象转换成Data对象

 @param jsonClass Json对象
 @return Data对象
 */
+ (NSData *)jsonClassConvertToJosnDataWithJsonClass:(id)jsonClass
{
    NSError *error;
    NSData *dataJson = [NSJSONSerialization dataWithJSONObject:jsonClass options:NSJSONWritingPrettyPrinted error:&error];
    
    return dataJson;
}

/**
 Data对象换成Json对象转
 
 @param jsonData Json对象
 @return json对象
 */
+ (id)jsonDataConvertToJosnClassWithJsonData:(NSData *)jsonData
{
    NSError *error;
    id jsonClass = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    
    return jsonClass;
}




/**
 数据四舍五入保留小数处理

 @param price 数据
 @param position 小数位置
 @return 值
 */
+ (NSString *)notRounding:(double)price afterPoint:(int)position
{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithDouble:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    
    return [NSString stringWithFormat:@"%@",roundedOunces];
}



/**
 *  升序,交换数据位置
 *
 *  @param array 修改之前的数据数组
 *
 *  @return 升序排好的数据数组
 */
+ (NSArray *)riseSortExchangeDataWithArray:(NSArray *)array
{
    NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        float val1 = [obj1 floatValue] ;
        float val2 = [obj2 floatValue] ;
        if(val1 < val2)
        {
            return NSOrderedAscending ;
        }
        else
        {
            return NSOrderedDescending ;
        }
    }];
    return resultArray ;
}


/**
 *  降序,交换数据位置
 *
 *  @param array 修改之前的数据数组
 *
 *  @return 降序排好的数据数组
 */
+ (NSArray *)dropSortExchangeDataWithArray:(NSArray *)array
{
    NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        float val1 = [obj1 floatValue] ;
        float val2 = [obj2 floatValue] ;
        if(val1 < val2)
        {
            return NSOrderedDescending ;
        }
        else
        {
            return NSOrderedAscending ;
        }
    }];
    return resultArray ;
}



/**
 *  升序,冒泡,交换数据位置
 *
 *  @param array 修改之前的数据数组
 *
 *  @return 升序排好的数据数组
 */
+ (NSArray *)riseSortWithArray:(NSArray *)array
{
    NSMutableArray *resultArray = [NSMutableArray arrayWithArray:array] ;
    for(int i = 0 ; i < resultArray.count ; i++)
    {
        for(int j = 0 ; j < resultArray.count-1 ; j++)
        {
            // 获取数据
            NSDictionary *dic1 = resultArray[j] ;
            NSInteger index1 =  [dic1[@"number"] integerValue];
            
            NSDictionary *dic2 = resultArray[j+1] ;
            NSInteger index2 =  [dic2[@"number"] integerValue];
            
            if(index1>index2)
            {
                [resultArray exchangeObjectAtIndex:j withObjectAtIndex:j+1] ;
            }
        }
    }
    return [resultArray copy] ;
}


/**
 *  降序,冒泡,交换数据位置
 *
 *  @param array 修改之前的数据数组
 *
 *  @return 降序排好的数据数组
 */
+ (NSArray *)dropSortWithArray:(NSArray *)array
{
    NSMutableArray *resultArray = [NSMutableArray arrayWithArray:array] ;
    for(int i = 0 ; i < resultArray.count ; i++)
    {
        for(int j = 0 ; j < resultArray.count-1 ; j++)
        {
            // 获取数据
            NSDictionary *dic1 = resultArray[j] ;
            NSInteger index1 =  [dic1[@"number"] integerValue];
            
            NSDictionary *dic2 = resultArray[j+1] ;
            NSInteger index2 =  [dic2[@"number"] integerValue];
            
            if(index1<index2)
            {
                [resultArray exchangeObjectAtIndex:j withObjectAtIndex:j+1] ;
            }
        }
    }
    return [resultArray copy] ;
}



/// model数据整合
/// @param result 数组原始数据
/// @param modelName 类名
+ (NSMutableArray *)arrayModelDateAssemblyWithResult:(NSArray *)result modelName:(NSString *)modelName
{
    NSArray *array = result;
    
    NSMutableArray *array_m = [[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in array)
    {
        
        id model = [[NSClassFromString(modelName) alloc]initWithContentDic:dic];
        
        [array_m addObject:model];
    }
    
    return array_m;
}

@end
